package io.gitee.devaper.starlight.security

import com.fasterxml.jackson.annotation.JsonIgnore
import io.gitee.devaper.starlight.common.entity.po.*
import org.springframework.security.core.*
import org.springframework.security.core.userdetails.UserDetails

/**
 * 根据User扩展的更加丰富符合项目需求的UserDetails
 *
 * @param user 用户信息对象
 */
open class DefaultUserDetails(
    val user: UserPO
) : UserDetails {

    @JsonIgnore
    override fun getAuthorities(): Collection<GrantedAuthority> {
        return emptyList()
    }

    @JsonIgnore
    override fun getPassword(): String {
        return user.password
    }

    @JsonIgnore
    override fun getUsername(): String {
        return user.username
    }

    @JsonIgnore
    override fun isAccountNonExpired(): Boolean {
        return true
    }

    @JsonIgnore
    override fun isAccountNonLocked(): Boolean {
        return true
    }

    @JsonIgnore
    override fun isCredentialsNonExpired(): Boolean {
        return true
    }

    @JsonIgnore
    override fun isEnabled(): Boolean {
        return true
    }

    override fun hashCode(): Int {
        return user.id.hashCode()
    }

    override fun equals(other: Any?): Boolean {
        return other is DefaultUserDetails && other.user.id == user.id
    }

}